import { apiService } from './api';

// 任务相关类型定义
export interface Task {
  id: string;
  title: string;
  description: string;
  type: string;
  category: string;
  status: 'created' | 'assigned' | 'in_progress' | 'suspended' | 'completed' | 'cancelled' | 'rejected';
  priority: number;
  assigneeId: string;
  assigneeName: string;
  createdBy: string;
  createdByName: string;
  updatedBy?: string;
  updatedByName?: string;
  businessKey?: string;
  businessType?: string;
  businessData?: Record<string, any>;
  processInstanceId?: string;
  processDefinitionId?: string;
  processDefinitionKey?: string;
  processTaskId?: string;
  formId?: string;
  formDataId?: string;
  startTime?: string;
  dueTime?: string;
  completedTime?: string;
  createdAt: string;
  updatedAt: string;
  tags?: string;
  tagList?: string[];
  remark?: string;
  extend?: Record<string, any>;
  sort?: number;
  deleted?: boolean;
  tenantId?: string;
  candidateUsers?: string[];
  candidateGroups?: string[];
  duration?: number;
  overdue?: boolean;
  remainingTime?: number;
  processInfo?: ProcessInfo;
  formInfo?: FormInfo;
  taskHistories?: TaskHistory[];
  taskComments?: TaskComment[];
}

// 流程信息
export interface ProcessInfo {
  processInstanceId: string;
  processDefinitionId: string;
  processDefinitionKey: string;
  processDefinitionName: string;
  processDefinitionVersion: string;
  businessKey: string;
  startUserId: string;
  startUserName: string;
  startTime: string;
  endTime?: string;
  status: string;
}

// 表单信息
export interface FormInfo {
  formId: string;
  formName: string;
  formTitle: string;
  formDataId: string;
  formUrl: string;
}

export interface TaskComment {
  id: string;
  content: string;
  author: string;
  authorAvatar?: string;
  createdAt: string;
}

export interface TaskHistory {
  id: string;
  action: string;
  description: string;
  operator: string;
  createdAt: string;
}

export interface TaskQueryParams {
  page?: number;
  size?: number;
  status?: string;
  priority?: string;
  assignee?: string;
  workflowId?: string;
  keyword?: string;
  startDate?: string;
  endDate?: string;
}

export interface TaskCreateRequest {
  title: string;
  description: string;
  type: string;
  category: string;
  priority: number;
  assigneeId: string;
  assigneeName?: string;
  businessKey?: string;
  businessType?: string;
  businessData?: Record<string, any>;
  processInstanceId?: string;
  processDefinitionId?: string;
  processDefinitionKey?: string;
  processTaskId?: string;
  formId?: string;
  formDataId?: string;
  startTime?: string;
  dueTime?: string;
  tags?: string[];
  remark?: string;
  extend?: Record<string, any>;
}

export interface TaskUpdateRequest {
  title?: string;
  description?: string;
  type?: string;
  category?: string;
  priority?: number;
  assigneeId?: string;
  assigneeName?: string;
  businessData?: Record<string, any>;
  dueTime?: string;
  tags?: string[];
  remark?: string;
  extend?: Record<string, any>;
}

// 任务服务类
class TaskService {
  // 获取任务列表
  async getTasks(params?: TaskQueryParams): Promise<{
    content: Task[];
    totalElements: number;
    totalPages: number;
    size: number;
    number: number;
  }> {
    return apiService.get('/tasks/list', params);
  }

  // 获取任务详情
  async getTask(id: string): Promise<Task> {
    return apiService.get(`/tasks/${id}`);
  }

  // 创建任务
  async createTask(task: TaskCreateRequest): Promise<Task> {
    return apiService.post('/tasks', task);
  }

  // 更新任务
  async updateTask(id: string, task: TaskUpdateRequest): Promise<Task> {
    return apiService.put(`/tasks/${id}`, task);
  }

  // 删除任务
  async deleteTask(id: string): Promise<void> {
    return apiService.delete(`/tasks/${id}`);
  }

  // 批量删除任务
  async batchDeleteTasks(ids: string[]): Promise<void> {
    return apiService.post('/tasks/batch-delete', { ids });
  }

  // 更新任务状态
  async updateTaskStatus(id: string, status: string): Promise<Task> {
    return apiService.patch(`/tasks/${id}/status`, { status });
  }

  // 分配任务
  async assignTask(id: string, assignee: string): Promise<Task> {
    return apiService.patch(`/tasks/${id}/assign`, { assignee });
  }

  // 获取任务评论
  async getTaskComments(taskId: string): Promise<TaskComment[]> {
    return apiService.get(`/tasks/${taskId}/comments`);
  }

  // 添加任务评论
  async addTaskComment(taskId: string, content: string): Promise<TaskComment> {
    return apiService.post(`/tasks/${taskId}/comments`, { content });
  }

  // 获取任务历史
  async getTaskHistory(taskId: string): Promise<TaskHistory[]> {
    return apiService.get(`/tasks/${taskId}/history`);
  }

  // 获取我的待办任务
  async getMyTasks(params?: TaskQueryParams): Promise<{
    content: Task[];
    totalElements: number;
    totalPages: number;
    size: number;
    number: number;
  }> {
    return apiService.get('/tasks/my-tasks', params);
  }

  // 获取任务统计信息
  async getTaskStatistics(): Promise<{
    total: number;
    created: number;
    assigned: number;
    in_progress: number;
    suspended: number;
    completed: number;
    cancelled: number;
    rejected: number;
    overdue: number;
  }> {
    return apiService.get('/tasks/statistics');
  }

  // 完成任务
  async completeTask(id: string, result?: any): Promise<Task> {
    return apiService.post(`/workflow/tasks/${id}/complete`, result);
  }

  // 驳回任务
  async rejectTask(id: string, reason: string): Promise<Task> {
    return apiService.post(`/tasks/${id}/reject`, { reason });
  }

  // 转办任务
  async transferTask(id: string, assignee: string, reason?: string): Promise<Task> {
    return apiService.post(`/tasks/${id}/transfer`, { assignee, reason });
  }

  // 获取任务分类列表
  async getTaskCategories(): Promise<string[]> {
    return apiService.get('/tasks/categories');
  }

  // 获取任务类型列表
  async getTaskTypes(): Promise<string[]> {
    return apiService.get('/tasks/types');
  }

  // 获取任务标签列表
  async getTaskTags(limit?: number): Promise<string[]> {
    const params = limit ? { limit } : {};
    return apiService.get('/tasks/tags', { params });
  }
}

export const taskService = new TaskService();